/**
 * File containing all the View classes
 */

/**
 * Basic View class
 * @type {omplusClass}
 */
Omplus.views.Component = Omplus.base.extend(Omplus.base.Base, {

    init:function () {
        if (this.model && this.controller) {
            var helper;
            var functionName;
            var controller = Omplus.globals.getObject(this.controller);
            for (var i in this.events) {
                helper = Omplus.base.create(Omplus.events[this.events[i]], {
                    omplusName:this.omplusName + "." + this.events[i],
                    sender:this.omplusName
                });
                functionName = this.events[i].charAt(0).toLowerCase() + this.events[i].slice(1);
                helper.attach(controller[functionName]);
            }
            Omplus.globals.getObject(this.omplusName + ".OnCreateView").notify([this.controller, this.model]);
        }
    },

    /**
     * Extending a simple div tag because we need new div tag each time
     * (instead, if we used just $('<div></div>') we would get a reference and not a new object)
     */
    render:function () {
        if (this.content == null)
            this.content = jQuery.extend(true, {}, $("<div></div>"));
        this.content.css(this.css);
        for (var i in this.cssClasses) {
            this.getContent().addClass(this.cssClasses[i]);
        }
        if (this.width) {
            this.content.css({width:this.width});
        }
        if (this.height) {
            this.content.css({height:this.height});
        }
    },

    destroy:function () {
        if (this.controller)
            Omplus.globals.getObject(this.omplusName + ".OnDestroyView").notify(this.controller);
        for (var i in this.events) {
            delete Omplus.globals.objects[this.omplusName + "." + this.events[i]];
        }
        delete Omplus.globals.objects[this.omplusName];
    },

    omplusName:null,

    omplusType:"Component",

    /**
     * Model connected to this view
     * @type {omplusClass}
     */
    model:null,

    controller:null,

    /**
     * Content of this Component
     * @type {jQuery}
     */
    content:null,

    /**
     * Css used for this Component
     * @type {Object}
     */
    css:null,

    cssClasses:[],

    events:["OnCreateView", "OnDestroyView", "OnUpdateView"],

    width:null,

    height:null,

    privilege: null,

    hasPrivilege : function(){
        if (this.privilege == null) return true;
        if (Omplus.globals.getObject("User.functionalities." + this.privilege)) return true;
        return false;
    },

    getContent:function () {
        return this.content;
    },

    getComponents:function () {
    },

    getModel:function () {
        return this.model;
    },

    setModel:function (model) {
        this.model = model;
    },

    getValue:function () {
    },

    setValue:function (newValue) {
    }
});

Omplus.views.ErrorControl = Omplus.base.extend(Omplus.views.Component, {

    init:function () {
    },

    /**
     * Adds a class to content and adds error label it's possible
     */
    render:function () {
        var error = jQuery.extend(true, {}, $("<label></label>"));
        error.addClass("error");
        this.content = error;
    },

    omplusName:null,

    omplusType:"ErrorControl",

    /**
     * Flag that tells us if this Component should check validity of its value
     * @type {boolean}
     */
    errorable:true,

    getComponents:function () {
        return null;
    },

    getValue:function () {
        return this.content.text();
    },

    setValue:function (newValue) {
        this.content.text(newValue);
    },

    setError:function (errorValue) {
    },

    emptyError:function () {
    }
});

/**
 * Basic Control class
 * @type {omlusClass}
 */
Omplus.views.Control = Omplus.base.extend(Omplus.views.Component, {

    init:function () {
    },

    /**
     * Adds a class to content and adds error label it's possible
     */
    render:function () {
        this.getContent().addClass(Omplus.constants.control);
        if (this.errorable) {
            var error = Omplus.base.create(Omplus.views.ErrorControl, {
                omplusName: this.omplusName + "ErrorControl"
            });
            this.getContent().append(error.getContent());
        }
    },

    omplusName:null,

    omplusType:"Control",

    /**
     * Flag that tells us if this Component should check validity of its value
     * @type {boolean}
     */
    errorable:true,

    getComponents:function () {
        return [this.omplusName];
    },

    setError:function (errorValue) {
        if (this.errorable) {
            if (Omplus.globals.getObject(this.omplusName + "ErrorControl")){
                Omplus.globals.getObject(this.omplusName + "ErrorControl").destroy();
                this.content.find(".error").remove()
            }
            var error = Omplus.base.create(Omplus.views.ErrorControl, {
                omplusName: this.omplusName + "ErrorControl",
                controller:"languageController",
                model: "Assets.list." + errorValue + ".localValue"
            });
            error.getContent().prependTo(this.getContent());
            error.setValue(Omplus.globals.getObject("Assets.list." + errorValue + ".localValue").getValue());
        }
    },

    emptyError:function () {
        if (this.errorable) {
            if (Omplus.globals.getObject(this.omplusName + "ErrorControl")){
                Omplus.globals.getObject(this.omplusName + "ErrorControl").destroy();
                this.content.find(".error").remove()
            }
            var error = Omplus.base.create(Omplus.views.ErrorControl, {
                omplusName: this.omplusName + "ErrorControl"
            });
            error.getContent().prependTo(this.getContent());
            error.setValue("");
        }
    },
    appendComponents : function(){}
});

/**
 * Basic Container class
 * @type {omplusClass}
 */
Omplus.views.Container = Omplus.base.extend(Omplus.views.Component, {

    /**
     * Adds a 'control' CSS class to this View
     */
    render:function () {
        this.content.addClass(Omplus.constants.container);
        if (!this.layout || !this.layout["omplusType"]) {
            this.layout = {
                omplusType:"HorizontalLayout"
            }
        }
        if (!this.layout.aligment) this.layout.aligment = "center";
        this.layout.omplusName = this.omplusName + ".Layout";
        this.layout.content = this.content;
        var layout = Omplus.base.create(Omplus.layouts[this.layout.omplusType], this.layout);
        for (var i in this.components) {
            if (typeof this.components[i] == Omplus.constants.object && this.components[i] != null) {
                Omplus.base.create(Omplus.views[this.components[i].omplusType], this.components[i]);
                this.components[i] = this.components[i].omplusName;
            }
        }
    },

    layout:null,

    height:"auto",

    width:"100%",

    omplusType:"Container",

    components:[],

    /**
     * omplusType to witch this label is added
     * @type {String}
     *
     */
    renderTo:null,

    /**
     * Object that holds the animation type and animation options for showing
     * @type {Object}
     */
    showEffect:null,

    /**
     * Object that holds the animation type and animation options for hiding
     * @type {Object}
     */
    hideEffect:null,

    destroy:function () {
        for (var i in this.components) {
            Omplus.globals.getObject(this.components[i]).destroy()
        }
        if (this.controller)
            Omplus.globals.getObject(this.omplusName + ".OnDestroyView").notify(this.controller);
        for (var i in this.events) {
            delete Omplus.globals.objects[this.omplusName + "." + this.events[i]];
        }
        delete Omplus.globals.objects[this.omplusName];
    },

    show:function () {
        this.getContent().hide();
        this.onRenderStart();
        Omplus.globals.getObject(this.getRenderTo()).append(this.omplusName);
        this.onRendering();
        var content = this.getContent();
        if (this.showEffect == null) {
            this.showEffect = {
                name:"fade"
            }
        }
        if (!this.showEffect["options"]) {
            this.showEffect["options"] = {};
        }
        var time;
        switch (this.showEffect.name) {
            default:
                time = 1000;
                if (this.showEffect["time"]) {
                    time = this.showEffect["time"];
                }
                var helper = this;
                content.show(this.showEffect.name, this.showEffect["options"], time, function () {
                    helper.onRenderFinish();
                });
                break;
        }
    },

    onRenderStart:function () {
    },

    onRendering:function () {
    },

    onRenderFinish:function () {
    },

    hide:function () {
        this.onCloseStart();
        var content = this.getContent();
        this.onClosing();
        if (this.hideEffect == null) {
            this.hideEffect = {
                name:"fade"
            }
        }
        if (!this.hideEffect["options"]) {
            this.hideEffect["options"] = {};
        }
        switch (this.hideEffect.name) {
            default:
                time = 1000;
                if (this.hideEffect["time"]) {
                    time = this.hideEffect["time"];
                }
                var helper = this;
                content.hide(this.hideEffect.name, this.hideEffect["options"], time, function () {
                    helper.onCloseFinish();
                });
                break;
        }
    },

    onCloseStart:function () {
    },

    onClosing:function () {
    },

    onCloseFinish:function () {
    },

    /**
     * Returns all components that are contained in this Container
     * @return {Array}
     */
    getComponents:function () {
        var components = [];
        var inner_components;
        for (var i in this.components) {
            if (this.components[i] != null) {
                inner_components = Omplus.globals.getObject(this.components[i]).getComponents();
                for (var j in inner_components) {
                    components.push(inner_components[j]);
                }
            }
        }
        components.push(this.omplusName);
        return components;
    },

    clear:function () {
        this.content.html("");
        Omplus.globals.getObject(this.omplusName + ".Layout").organize();
    },

    appendComponents : function(){
        this.clear();
        if (this.hasPrivilege())
        for (var i in this.components) {
            if (Omplus.globals.getObject(this.components[i]).hasPrivilege())
                this.append(this.components[i]);
        }
    },

    append:function (component) {
        Omplus.globals.getObject(component).appendComponents();
        if (this.hasPrivilege()){
            Omplus.globals.getObject(this.omplusName + ".Layout").append(component);
        }
    },

    getRenderTo:function () {
        return this.renderTo;
    },
    emptyError:function () {
    }
});



